FUNCTION ZFIFM_INV_SYNCFILE.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_JSDDH) TYPE  ZFIT079_HEAD-JSDDH
*"     VALUE(IV_ZYSFPHM) TYPE  ZFIT079_ITEM-ZYSFPHM
*"     VALUE(IV_URL) TYPE  ZFIT078_HEAD-XML_URL
*"     VALUE(IV_DOC_TYPE) TYPE  TOADD-DOC_TYPE
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"  TABLES
*"      RETURN STRUCTURE  BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
  DATA: LO_HTTP_CLIENT TYPE REF TO IF_HTTP_CLIENT.
  DATA: LR_REQUEST TYPE REF TO IF_HTTP_REQUEST.
  DATA:
    LV_HTTP_STATUS TYPE I,
    LV_SYSUBRC     TYPE I,
    LV_ERROR_MSG   TYPE STRING,
    LV_URL         TYPE STRING.

  ZIRPSAVE1: 'ZFIFM_INV_SYNCFILE'. "Log

  CLEAR ES_MSG.
  " LV_URL = 'https://storage.nuonuo.com/linkDownload/v0tVtRSmWqtAVQ5sAnCvFD2b-6DOn7qw6o08dNsZeEZdB3ju7MFLfDZkPgeOQ7qC.pdf'.

  LV_URL = IV_URL.
  CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
    EXPORTING
      URL    = LV_URL
*     PROXY_HOST    = 'proxy'
*     PROXY_SERVICE = '3128'
    IMPORTING
      CLIENT = LO_HTTP_CLIENT.

  LR_REQUEST = LO_HTTP_CLIENT->REQUEST.
  LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = 'Content-Type' VALUE = 'application/json' ).
  LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = 'charset' VALUE = 'UTF-8' ).
  LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = 'Accept' VALUE = '*/*' ).
  LO_HTTP_CLIENT->REQUEST->SET_HEADER_FIELD( NAME = '~request_method' VALUE = IF_HTTP_REQUEST=>CO_REQUEST_METHOD_GET ).

  CALL METHOD LO_HTTP_CLIENT->SEND
    EXCEPTIONS
      HTTP_COMMUNICATION_FAILURE = 1
      HTTP_INVALID_STATE         = 2
      HTTP_PROCESSING_FAILED     = 3.

  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
          INTO ES_MSG-MESSAGE
          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    APPEND ES_MSG TO RETURN.
    ZIRPSAVE2: CX_R SPACE.
    RETURN.
  ELSE.

    CALL METHOD LO_HTTP_CLIENT->RECEIVE
      EXCEPTIONS
        HTTP_COMMUNICATION_FAILURE = 1
        HTTP_INVALID_STATE         = 2
        HTTP_PROCESSING_FAILED     = 3.
    IF SY-SUBRC <> 0.
      CALL METHOD LO_HTTP_CLIENT->GET_LAST_ERROR
        IMPORTING
          CODE    = LV_SYSUBRC
          MESSAGE = LV_ERROR_MSG.

      ES_MSG-TYPE = 'E'.
      ES_MSG-MESSAGE = LV_ERROR_MSG.

      APPEND ES_MSG TO RETURN.
      ZIRPSAVE2: CX_R SPACE.
      RETURN.

    ELSE.
      DATA: LV_FILEDATA TYPE XSTRING.
*---提取接口返回的数据
      "LV_RESP_JSON = LO_HTTP_CLIENT->RESPONSE->GET_CDATA( ).
      LV_FILEDATA = LO_HTTP_CLIENT->RESPONSE->GET_DATA( ).
    ENDIF.
  ENDIF.
  LO_HTTP_CLIENT->CLOSE( ).


  DATA:
    LV_LEN           TYPE SAPB-LENGTH,
    LV_LENGTH        TYPE I,
    LV_ARCHIV_DOC_ID TYPE SAPB-SAPADOKID,
    LS_RETURN        TYPE BAPIRET2,
    LT_BIN_DATA      TYPE TABLE OF TBL1024 WITH HEADER LINE.

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      BUFFER        = LV_FILEDATA
*     APPEND_TO_TABLE       = ' '
    IMPORTING
      OUTPUT_LENGTH = LV_LENGTH
    TABLES
      BINARY_TAB    = LT_BIN_DATA[].

  LV_LEN = LV_LENGTH.

  CALL FUNCTION 'ARCHIVOBJECT_CREATE_TABLE'
    EXPORTING
      ARCHIV_ID                = 'Z1'
*     ARC_DOC_ID               =
      DOCUMENT_TYPE            = IV_DOC_TYPE
      LENGTH                   = LV_LEN
*     COMPID                   = 'data'
*     SIGNATURE                = 'X'
*     VSCAN_PROFILE            = '/SCMS/KPRO_CREATE'
    IMPORTING
      ARCHIV_DOC_ID            = LV_ARCHIV_DOC_ID
    TABLES
      BINARCHIVOBJECT          = LT_BIN_DATA[]
    EXCEPTIONS
      ERROR_ARCHIV             = 1
      ERROR_COMMUNICATIONTABLE = 2
      ERROR_KERNEL             = 3
      BLOCKED_BY_POLICY        = 4
      OTHERS                   = 5.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
          INTO ES_MSG-MESSAGE
          WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    APPEND ES_MSG TO RETURN.
    ZIRPSAVE2: CX_R SPACE.
    RETURN.
  ENDIF.

  DATA:
    LV_AR_OBJECT TYPE TOAOM-AR_OBJECT,
    LV_OBJECT_ID TYPE SAPB-SAPOBJID,
    LV_FILE_NAME TYPE CHAR255.


  LV_AR_OBJECT = 'ZINV_' && IV_DOC_TYPE.
  LV_OBJECT_ID = IV_JSDDH.
  LV_FILE_NAME = IV_ZYSFPHM && '_ZINV_' && IV_DOC_TYPE && '.' && IV_DOC_TYPE.

  CALL METHOD /OTX/PF00_CL_UTILITIES=>ARCHIVE_CONNECTION_INSERT "BOB-4005
    EXPORTING
      PI_ARC_DOC_ID = LV_ARCHIV_DOC_ID
      PI_AR_DATE    = SY-DATUM
      PI_AR_OBJECT  = LV_AR_OBJECT   "归档文件类型，比如ZINV_PDF
      PI_OBJECT_ID  = LV_OBJECT_ID   "业务单号
      PI_SAP_OBJECT = GC_SAP_OBJECT  "'ZZFI_FPJSD'.
      PI_DOC_TYPE   = IV_DOC_TYPE    "附件类型，比如PDF、XML等
      PI_FILENAME   = LV_FILE_NAME
      PI_CREATOR    = SY-UNAME
    IMPORTING
      PES_RETURN    = LS_RETURN.

  IF LS_RETURN IS NOT INITIAL.
    LS_RETURN-TYPE = 'E'.
    ES_MSG = LS_RETURN.
  ELSE.
    ES_MSG-TYPE = 'S'.
    ES_MSG-MESSAGE = |{ '发票附件已经成功上传-'(T01) } { IV_DOC_TYPE } |.
  ENDIF.

  APPEND ES_MSG TO RETURN.
  ZIRPSAVE2: CX_R SPACE.
ENDFUNCTION.
